/*
 * DimensionLabel.java
 */

package xq.swing;

import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

/**
 *
 * @author ThorntonRP
 */
public class DimensionLabel extends JLabel {

	private static final long serialVersionUID = 6967896830481425117L;
	
	private String prefix;

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		String oldPrefix = this.prefix;
		this.prefix = prefix;
		updateText();
		firePropertyChange("prefix", oldPrefix, prefix);
	}

	public DimensionLabel() {
		setHorizontalAlignment(SwingConstants.CENTER);
		setBorder(BorderFactory.createLineBorder(UIManager.getDefaults().getColor("controlShadow")));
		setPreferredSize(new Dimension(100, 100));
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				updateText();
			}
		});
	}

	private void updateText() {
		if (prefix == null) {
			setText("<html><center>[" + getWidth() + ", " + getHeight() + "]");
		} else {
			setText("<html><center>" + prefix + "<br>[" + getWidth() + ", " + getHeight() + "]");
		}
	}
}
